#!/usr/bin/env node

module.exports = function (context) {
    var http = require('http');
    var fs = require('fs');

    var path         = context.requireCordovaModule('path'),
        fs           = context.requireCordovaModule('fs'),
        shell        = context.requireCordovaModule('shelljs'),
        projectRoot  = context.opts.projectRoot,
        ConfigParser = context.requireCordovaModule('cordova-lib/src/configparser/ConfigParser'),
        config       = new ConfigParser(path.join(context.opts.projectRoot, "config.xml")),
        packageName = config.android_packageName() || config.packageName();

    console.info("Running ios-install.Hook: " + context.hook + ", Package: " + packageName + ", Path: " + projectRoot + ".");

    if (!packageName) {
        console.error("Package name could not be found!");
        return ;
    }

    // android platform available?
    if (context.opts.cordova.platforms.indexOf("ios") === -1) {
        console.info("IOS platform has not been added.");
        return ;
    }

    var targetDir  = path.join(projectRoot, "plugins", "com.wingsfrog.zc-video", "src", "ios");
        targetFile = path.join(targetDir, "UMEyeCloudIOS.framework");
    if (['before_plugin_uninstall'].indexOf(context.hook) !== -1) {
        // remove it
        try {
            fs.unlinkSync(targetFile);
        } catch (err) {}
    } else {
        shell.mkdir('-p', targetDir);
        shell.exec('wget http://7xpm80.dl1.z0.glb.clouddn.com/UMEyeCloudIOS.framework.zip -P ' + targetDir);
        shell.exec('unzip ' + targetFile + ' -d ' + targetDir);
        shell.rm(targetFile + ".zip");
    }
};
